package cn.wolfcode.luowowo.mgrsite.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.StrategyTagQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    @RequestMapping("input")
    public String input(Long id, Model model) {
        if (id != null) {
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            model.addAttribute("strategyDetail", strategyDetail);
            String tags = strategyTagService.queryByDetailId(id);
            model.addAttribute("tags", tags);
        }
        List<Strategy> strategies = strategyService.list();
        model.addAttribute("strategies", strategies);
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes", themes);
        return "strategyDetail/input";
    }

    @RequestMapping("getCatalogByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogService.getCatalogByStrategyId(strategyId);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AJaxResult saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        strategyDetailService.saveOrUpdate(strategyDetail, tags);
        return AJaxResult.SUCCESS();
    }

    @RequestMapping("changeState")
    @ResponseBody
    public AJaxResult changeState(int state, Long id){
        strategyDetailService.changeState(state,id);
        return AJaxResult.SUCCESS();
    }
}
